package com.example.lsrsbackend.config;


import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
public class RabbitMQConfig {

    //座位预约请求队列
    @Bean
    public Queue seatReservationQueue() {
        return QueueBuilder.durable("seat.reservation.queue")
                .withArgument("x-dead-letter-exchange","dlx.exchange")
                .withArgument("x-dead-letter-routing-key","seat.reservation.dlq")
                .build();
    }


    //座位状态更新队列
    @Bean
    public Queue seatStatusUpdateQueue() {
        return new Queue("seat.status.update.queue", true);
    }


    //预约延迟队列（处理超时未签到）
    @Bean
    public Queue seatReservationDelayQueue() {
        return QueueBuilder.durable("seat.reservation.delay.queue")
                .withArgument("x-dead-letter-exchange", "seat.reservation.timeout.exchange")
                .withArgument("x-dead-letter-routing-key", "seat.reservation.timeout")
                .withArgument("x-message-ttl", 30 * 60 * 1000) // 30分钟
                .build();
    }

    //预约超时处理队列
    @Bean
    public Queue seatReservationTimeoutQueue() {
        return new Queue("seat.reservation.timeout.queue", true);
    }


    //交换机
    @Bean
    public DirectExchange seatExchange() {
        return new DirectExchange("seat.exchange");
    }

    @Bean
    public DirectExchange seatTimeoutExchange() {
        return new DirectExchange("seat.reservation.timeout.exchange");
    }

    //绑定队列和交换机
    @Bean
    public Binding bindingReservation(Queue seatReservationQueue, DirectExchange seatExchange) {
        return BindingBuilder.bind(seatReservationQueue).to(seatExchange).with("reservation");
    }

    @Bean
    public Binding bindingStatusUpdate(Queue seatStatusUpdateQueue, DirectExchange seatExchange) {
        return BindingBuilder.bind(seatStatusUpdateQueue).to(seatExchange).with("status.update");
    }

    @Bean
    public Binding bindingTimeout(Queue seatReservationTimeoutQueue, DirectExchange seatTimeoutExchange) {
        return BindingBuilder.bind(seatReservationTimeoutQueue).to(seatTimeoutExchange).with("seat.reservation.timeout");
    }

    // JSON消息转换器
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }


}
